<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" style="width: 800px">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
      :inline="true"
      v-loading="formLoading"
    >
      <el-form-item label="项目名称" prop="projectNo">
        <el-select
          v-model="formData.projectNo"
          filterable
          placeholder="请输入查询的客户"
          @change="projectChange"
          style="width: 168px"
        >
          <el-option
            v-for="item in projectList"
            :key="item.projectNo"
            :label="item.projectName"
            :value="item.projectNo"
          />
        </el-select>
<!--        <el-input v-model="formData.projectNo" placeholder="请输入项目编码" />-->
      </el-form-item>
      <el-form-item label="线路名称" prop="lineName">
        <el-input v-model="formData.lineName" placeholder="请输入线路名称" />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="formData.customerName" placeholder="请输入客户名称" disabled />
      </el-form-item>
      <el-form-item label="客户信用代码" prop="customerUscc">
        <el-input v-model="formData.customerUscc" placeholder="请输入客户统一信用代码" disabled />
      </el-form-item>
<!--      <el-form-item label="客户类型" prop="customerType">-->
<!--        <el-radio-group v-model="formData.customerType">-->
<!--          <el-radio value="1">请选择字典生成</el-radio>-->
<!--        </el-radio-group>-->
<!--      </el-form-item>-->

      <el-form-item label="发货单位名称" prop="consignor">
        <el-select
          v-model="formData.consignor"
          filterable
          placeholder="请输入查询的发货单位"
          @change="consignorChange"
          style="width: 168px"
        >
          <el-option
            v-for="item in sendList"
            :key="item.contactsUscc"
            :label="item.contactsName"
            :value="item.contactsUscc"
          />
        </el-select>
<!--        <el-input v-model="formData.consignor" placeholder="请输入发货单位名称" />-->
      </el-form-item>
      <el-form-item label="收货单位名称" prop="consignee">
        <el-select
          v-model="formData.consignee"
          filterable
          placeholder="请输入查询的发货单位"
          @change="consigneeChange"
          style="width: 168px"
        >
          <el-option
            v-for="item in receiptList"
            :key="item.contactsUscc"
            :label="item.contactsName"
            :value="item.contactsUscc"
          />
        </el-select>
<!--        <el-input v-model="formData.consignee" placeholder="请输入收货单位名称" />-->
      </el-form-item>
      <el-form-item label="发货信用代码" prop="consignorUscc">
        <el-input v-model="formData.consignorUscc" placeholder="请输入发货单位统一社会信用代码" disabled />
      </el-form-item>
      <el-form-item label="收货信用代码" prop="consigneeUscc">
        <el-input v-model="formData.consigneeUscc" placeholder="请输入收货单位统一社会信用代码" disabled />
      </el-form-item>
      <el-form-item label="发货联系人" prop="consignorName">
        <el-input v-model="formData.consignorName" placeholder="请输入发货联系人" disabled />
      </el-form-item>
      <el-form-item label="收货联系人" prop="consigneeName">
        <el-input v-model="formData.consigneeName" placeholder="请输入收货联系人" disabled />
      </el-form-item>
      <el-form-item label="发货联系人电话" prop="consignorPhone">
        <el-input v-model="formData.consignorPhone" placeholder="请输入发货联系人电话" disabled />
      </el-form-item>
      <el-form-item label="收货联系人电话" prop="consigneePhone">
        <el-input v-model="formData.consigneePhone" placeholder="请输入收货联系人电话" disabled />
      </el-form-item>
<!--      <el-form-item label="发货地区划代码" prop="consignorAddrCode">-->
<!--        <el-input v-model="formData.consignorAddrCode" placeholder="请输入发货地区划代码" />-->
<!--      </el-form-item>-->
      <el-form-item label="出发(装货)地址" prop="consignorAddr">
        <el-input v-model="formData.consignorAddr" placeholder="请输入出发(装货)地址" disabled />
      </el-form-item>
      <el-form-item label="目的(收货)地址" prop="consigneeAddr">
        <el-input v-model="formData.consigneeAddr" placeholder="请输入目的(收货)地址" disabled />
      </el-form-item>
      <el-form-item label="发货方详细地址" prop="consignorDetailAddr">
        <el-input v-model="formData.consignorDetailAddr" placeholder="请输入发货方详细地址" disabled />
      </el-form-item>
      <!--      <el-form-item label="收货地区划代码" prop="consigneeAddrCode">-->
      <!--        <el-input v-model="formData.consigneeAddrCode" placeholder="请输入收货地区划代码" />-->
      <!--      </el-form-item>-->
      <el-form-item label="收货方详细地址" prop="consigneeDetailAddr">
        <el-input v-model="formData.consigneeDetailAddr" placeholder="请输入收货方详细地址" disabled />
      </el-form-item>
      <el-form-item label="货物名称" prop="goodsName">
        <el-input v-model="formData.goodsName" placeholder="请输入货物名称" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { LineApi, LineVO } from '@/api/base/line'
import { ProjectApi } from '@/api/base/project'
import {CustomerContactApi} from "@/api/base/customercontact";
/** 线路管理 表单 */
defineOptions({ name: 'LineForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  projectNo: undefined,
  lineCode: undefined,
  lineName: undefined,
  lineType: undefined,
  customerUscc: undefined,
  customerType: undefined,
  customerName: undefined,
  consignor: undefined,
  consignorUscc: undefined,
  consignorName: undefined,
  consignorPhone: undefined,
  consignorAddrCode: undefined,
  consignorAddr: undefined,
  consignorDetailAddr: undefined,
  consignee: undefined,
  consigneeUscc: undefined,
  consigneeName: undefined,
  consigneePhone: undefined,
  consigneeAddrCode: undefined,
  consigneeAddr: undefined,
  consigneeDetailAddr: undefined,
  goodsName: undefined,
  deptId: undefined
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref
const projectList = ref([])
const sendList = ref([])
const receiptList = ref([])
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  projectList.value = await ProjectApi.getProjectList({})
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await LineApi.getLine(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as LineVO
    if (formType.value === 'create') {
      await LineApi.createLine(data)
      message.success(t('common.createSuccess'))
    } else {
      await LineApi.updateLine(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    projectNo: undefined,
    lineCode: undefined,
    lineName: undefined,
    lineType: undefined,
    customerUscc: undefined,
    customerType: undefined,
    customerName: undefined,
    consignor: undefined,
    consignorUscc: undefined,
    consignorName: undefined,
    consignorPhone: undefined,
    consignorAddrCode: undefined,
    consignorAddr: undefined,
    consignorDetailAddr: undefined,
    consignee: undefined,
    consigneeUscc: undefined,
    consigneeName: undefined,
    consigneePhone: undefined,
    consigneeAddrCode: undefined,
    consigneeAddr: undefined,
    consigneeDetailAddr: undefined,
    goodsName: undefined,
    deptId: undefined
  }
  formRef.value?.resetFields()
}

const projectChange = async (projectNo: string) => {
  const projectData = projectList.value.find((item) => {
    return item.projectNo === projectNo
  })
  formData.value.projectNo = projectData.projectNo
  formData.value.projectName = projectData.projectName
  formData.value.customerUscc = projectData.customerUscc
  formData.value.customerType = projectData.customerType
  formData.value.customerName = projectData.customerName
  const list = await CustomerContactApi.getCustomerContactList({
    customerUscc: projectData.customerUscc
  });
  sendList.value = list.filter((item) => item.contactsType == 0)
  receiptList.value = list.filter((item) => item.contactsType == 1)
}

const consignorChange = (contactsUscc: string) => {
  const sendData = sendList.value.find((item) => {
    return item.contactsUscc === contactsUscc
  })
  formData.value.consignor = sendData.contactsName
  formData.value.consignorUscc = sendData.contactsUscc
  formData.value.consignorName = sendData.contacts
  formData.value.consignorPhone = sendData.contactsPhone
  formData.value.consignorAddrCode = sendData.contactsAddrCode
  formData.value.consignorAddr = sendData.contactsAddr
  formData.value.consignorDetailAddr = sendData.contactsDetailAddr
}

const consigneeChange = (contactsUscc: string) => {
  const receiptData = receiptList.value.find((item) => {
    return item.contactsUscc === contactsUscc
  })
  formData.value.consignee = receiptData.contactsName
  formData.value.consigneeUscc = receiptData.contactsUscc
  formData.value.consigneeName = receiptData.contacts
  formData.value.consigneePhone = receiptData.contactsPhone
  formData.value.consigneeAddrCode = receiptData.contactsAddrCode
  formData.value.consigneeAddr = receiptData.contactsAddr
  formData.value.consigneeDetailAddr = receiptData.contactsDetailAddr
}

</script>
